package net.nio;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;

/**
 * 复制文件
 *
 * @author 王霄
 * @date 2021/8/21 17:15
 */
public class FileChannel_03 {
    public static void main(String[] args) throws IOException {

        //将文件读入buffer
        File file = new File("d:\\aaa.txt");
        var inputStream = new FileInputStream(file);
        var readChannel = inputStream.getChannel();
        var buffer = ByteBuffer.allocate((int) file.length());
        readChannel.read(buffer);

        //读写翻转
        buffer.flip();

        //将文件写入channel
        var outputStream = new FileOutputStream("d:\\bbb.txt");
        var writeChannel = outputStream.getChannel();
        writeChannel.write(buffer);


        outputStream.close();
        inputStream.close();
    }
}
